<?php

namespace app\commands;

use app\config\Constants;
use app\models\GoodsOrder;
use Carbon\Carbon;
use yii\console\Controller;

/**
 * 失效订单自动关闭
 * Class ExpireController
 * @package app\commands
 */
class ExpireController extends Controller {

    public function actionRun(){
        $orders = GoodsOrder::findAll(['status'=>Constants::ORDER_WAIT_PAYMENT]);
        foreach($orders as $v){
            //大于2个小时则关闭
            if(Carbon::now()->diffInHours(Carbon::make($v->add_time)) >= 2){
                $v->setAttribute("status",Constants::ORDER_CANCEL);
                $v->setAttribute("pay_status",Constants::PAY_STATUS_CANCEL);
                $v->update() !== false;
            }
        }
    }
}